package com.my.blog.website.controller.admin;

import com.my.blog.website.controller.BaseController;
import com.my.blog.website.modal.Vo.UploadVo;
import com.my.blog.website.service.IUploadService;
import com.my.blog.website.utils.Commons;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.OutputStream;

/**
 * <p>Title         :<br/></p>
 * <p>Description   :<br/></p>
 * <p>
 * Created by p on 2017/10/25.
 */
@Controller
@RequestMapping(value = "")
public class CommonController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadController.class);

    @Resource
    private IUploadService uploadService;

    @Resource
    private CacheManager cacheManager;

    @RequestMapping(value = "static/upload/{id}")
    public void findOne(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response) {
        //FileInputStream fis = null;
        //Closer closer = Closer.create();
        try {
            Cache cache = cacheManager.getCache("blogCache");
            byte[] data;
            data = cache.get(id.toString(),byte[].class);
            if(null==data){
                UploadVo uploadVo = uploadService.selectById(id);
                if (null == uploadVo) {
                    LOGGER.error("id={}的附件不存在",id);
                }

                //String uploadFile = PropertiesUtils.getProperty(WebConst.UPLOAD_BASE_PATH) + File.separator + uploadVo.getFkey();
                String uploadFile = Commons.getUploadBasePath() + File.separator + uploadVo.getFkey();

                data = Commons.getUploadFile(uploadFile);
                cache.put(id.toString(),data);
            }

            if (null == data){
                String msg = "id={}的附件已被删除";
                LOGGER.error(msg, id);
            }
            /*指定返回的数据类型*/
            /*if(Types.FILE_TYPE_IMAGE.getType().equals(uploadVo.getFtype())){
                response.setContentType("iamge/jpeg");
            }else if (Types.FILE_TYPE_VIDEO.getType().equals(uploadVo.getFtype())){
                response.setContentType("video/mp4"); // 设置返回的文件类型
            }else if(Types.FILE_TYPE_TEXT.getType().equals(uploadVo.getFtype())){
                response.setContentType("text/html"); // 设置返回的文件类型
            }*/
            OutputStream out = response.getOutputStream();

            //closer.register(out);
            out.write(data);
        } catch (Exception e) {
            String msg = "id={}的附件获取失败";
            LOGGER.error(msg, id);
        }finally {
            /*try {
                closer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }*/
        }
    }
}
